<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑字典'">
    <template #body>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">ID</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.id" disabled class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">父级ID</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.pid" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">分类标识</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.cat" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">编码</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.code" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">键名</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.key" class="d-form-textarea"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">键值</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.value" class="d-form-textarea"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">卷</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.volume" class="d-form-textarea"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">编</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.part" class="d-form-textarea"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">描述</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.description" class="d-form-textarea"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">详细</label>
          </div>
          <div class="d-form-el">
            <HtmlViewer v-model="state.explain" :mode="STATUS.EDIT" ref="htmlViewer" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">备注</label>
          </div>
          <div class="d-form-el">
            <HtmlViewer v-model="state.remark" :mode="STATUS.EDIT" ref="htmlViewer" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">等级标识</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.level" class="d-form-input" type="number" placeholder="0-10,6及以上为系统级,3-5为模块级,0-2为用户级" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">排序</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.order" class="d-form-input" type="text" placeholder="数值越大越靠前" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">状态</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.status" class="d-form-select">
              <option v-for="item in statusList" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.updateTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="修改时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">修改人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.updateUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.createTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="创建时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">创建人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.createUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
      </div>
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages'
import { Dialoger, FormCalendar } from '@daelui/vdog/dist/components'
import { HtmlViewer } from '@daelui/vdog/dist/viewer'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import { STATUS } from '@daelui/dogjs/dist/constant'
import dicSer from '../../service/action/dictionary.action.js'
import { state } from '@/service/stock/index.js'

export default {
  components: {
    PanerForm,
    FormCalendar,
    HtmlViewer
  },

  data () {
    return {
      STATUS,
      isEdit: false, // 是否是编辑模式
      // 表单数据
      state: {
        id: '',
        pid: '',
        level: 5,
        cat: '',
        code: '',
        key: '',
        value: '',
        volume: '',
        part: '',
        chapter: '',
        section: '',
        item: '',
        clause: '',
        article: '',
        detail: '',
        description: '',
        explain: '',
        remark: '',
        order: 1,
        status: 1,
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      },
      // 状态列表
      statusList: state.statusList
    }
  },

  methods: {
    /**
     * @function 加载笔记
    */
    loadItem (id) {
      dicSer.queryItem({ id }).then(result => {
        let data = result.data || {}
        this.state = data
        data.content = data.content || ''
        // 替换所有的换行符
        data.content = data.content.replace(/\r\n/g, '<br />')
        data.content = data.content.replace(/\n/g, '<br />')
        this.$refs.htmlViewer.update(data.content)
      })
    },

    /**
     * @function 提交
    */
    handleSubmit () {
      let defer = null
      // 编辑
      if (this.isEdit) {
        defer = dicSer.editItem(this.state)
      } else { // 添加
        defer = dicSer.addItem(this.state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
        } else {
          Dialoger.danger(locale.action.fail)
        }
        // 添加则回到列表页
        if (!this.isEdit) {
          this.$router.push(this.getMicroRoute('/dictionary/list'))
        }
      })
    },
    /**
     * @function 重置
    */
    handleReset () {

    }
  },

  beforeMount () {
    let params = this.$route.params
    // 新建
    if (!params.id) {
      this.isEdit = false
    } else { // 编辑
      this.isEdit = true
      this.loadItem(params.id)
    }
  },

  mounted () {

  }
}
</script>

<style lang="less" scoped>
.d-form-label {
  width: 90px;
}
.d-form-group /deep/.ql-container .ql-editor {
  min-height: 200px;
}
</style>
